package com.gitee.wsl.collections.iterator

import com.gitee.wsl.func.Supplier

class SupplierIterator<T>(
    private val supplier: Supplier<T>
) : Iterator<T> {
    private var item: T
    private var end = false

    init {
        this.item = supplier.get()
        if (item == null)
            end = true
    }

    override fun next(): T {
        if (!hasNext()) throw NoSuchElementException()
        val result = item
        item = supplier.get()
        if (item == null) end = true
        return result
    }

    override fun hasNext(): Boolean {
        return !end
    }
}